package vos.vn;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;  
import java.net.HttpURLConnection;  
import java.net.URL;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.TextView;  

public class WebService_HttpConnectionActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        TextView tv = new TextView(this);  
        
        StringBuilder sb = new StringBuilder(" tu said: ");
        String tErrorString = "";         
        OutputStreamWriter wr = null;
        HttpURLConnection conn = null;
        String datNearhere = "Token=9db3edcf-3408-4489-a4ee-21a34f615607&LocationID="+"";
        try{  
            URL url = new URL("http://www.littleemperors.com/json/GetCategories.aspx");  
            
            //open connection to server url
            conn = (HttpURLConnection) url.openConnection();
            if(conn!=null)
            	 sb.append("connection is successfull");
            
            
            //properties for outputing
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data;");
            conn.setRequestProperty("Transfer-Encoding", "chunked");
            conn.setRequestProperty("Content-length",""+ datNearhere.length());
            //conn.setRequestProperty(datNearhere, datNearhere);
            
           
            //get data
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
            //out.write(datNearhere);
            out.flush();
            out.close();
            
            sb.append("\n response message :" + conn.getResponseMessage()); //status code ok
            sb.append("\n response code :" + conn.getResponseCode() + "\n");  //// server reponse success. 200
            
            InputStreamReader in = new InputStreamReader(conn.getInputStream());
            BufferedReader buff = new BufferedReader(in);
            String line;
            do {
                line = buff.readLine();
                sb.append(line + "\n");
            } while (line != null);
            
        } catch (Exception e) {  
            tErrorString = e.getMessage();  
        } finally {
        	conn.disconnect();
        }
          
        if(!tErrorString.equals(""))
        	tv.setText(tErrorString);
        else
        	tv.setText(sb.toString());
        
        setContentView(tv);  
    }
}